package com.iman.manjusri.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.manjusri.domain.CourseCategoryBean;
import com.iman.manjusri.domain.CourseCtgExam;
import com.iman.manjusri.domain.CourseCtgHomework;
import com.iman.manjusri.domain.CourseInfoBean;
import com.iman.manjusri.domain.TopicCourseBean;
import com.iman.manjusri.mappers.CourseInfoMapper;
import com.iman.manjusri.mappers.DrmCourseMapper;
import com.iman.manjusri.mappers.models.DrmCourse;
import com.iman.manjusri.mappers.models.DrmCourseExample;
import com.iman.manjusri.service.CourseInfoService;
import com.iman.manjusri.vo.CourseCtgVo;
import com.iman.manjusri.vo.CourseRecommendVo;

@Service
public class CourseInfoServiceImpl implements CourseInfoService {

	@Inject
	CourseInfoMapper courseInfoMapper;

	@Inject
	DrmCourseMapper drmCourseMapper;

	@Override
	public List<CourseInfoBean> getCourseInfoList(Map<String, Object> searchMap) {
		return courseInfoMapper.getCourseInfoBeanList(searchMap);

	}

	@Override
	public CourseInfoBean getCourseInfo(Map<String, Object> searchMap) {
		CourseInfoBean bean = null;
		List<CourseInfoBean> courseList = courseInfoMapper.getCourseInfoBeanList(searchMap);
		if (courseList.size() > 0) {
			bean = courseList.get(0);
		}
		return bean;
	}

	@Override
	public List<CourseCategoryBean> getCourseCategoryBeanList(Map<String, Object> searchInfo) {

		return courseInfoMapper.getCourseCategoryBeanList(searchInfo);
	}

	@Override
	public List<CourseCtgExam> getCourseExamBeanList(Map<String, Object> searchInfo) {

		return courseInfoMapper.getCourseExamBeanList(searchInfo);
	}

	@Override
	public List<CourseCtgHomework> getCourseHomeworkBeanList(Map<String, Object> searchInfo) {

		return courseInfoMapper.getCourseHomeworkBeanList(searchInfo);
	}

	@Override
	public List<CourseCtgVo> getCourseCtgVoInfo(Map<String, Object> mapInfo) {
		List<CourseCtgVo> courseCtgVoList = new ArrayList<CourseCtgVo>();

		List<CourseCategoryBean> courseCategoryBeanList = courseInfoMapper.getCourseCategoryBeanList(mapInfo);
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		for (CourseCategoryBean bean : courseCategoryBeanList) {
			CourseCtgVo vo = new CourseCtgVo();
			long ctgId = bean.getId();
			hashMapParams.put("categoryId", ctgId);
			List<CourseCtgHomework> courseCtgHomeworkList = courseInfoMapper.getCourseHomeworkBeanList(hashMapParams);
			vo.setCourseCtgHomeworkList(courseCtgHomeworkList);
			List<CourseCtgExam> courseCtgExamList = courseInfoMapper.getCourseExamBeanList(hashMapParams);
			vo.setCourseCtgExamList(courseCtgExamList);
			courseCtgVoList.add(vo);

		}
		return courseCtgVoList;
	}

	@Override
	public List<TopicCourseBean> getTopicInfoList(Map<String, Object> mapInfo) {

		return courseInfoMapper.getTopicInfo(mapInfo);
	}

	@Override
	public List<CourseRecommendVo> getRecommendCourse(Map<String, Object> mapInfo) {

		return courseInfoMapper.getRecommendCourseInfoBeanList(mapInfo);
	}

	/**
	 * 根据ID 获取 课程信息
	 */
	@Override
	public List<DrmCourse> getCourseInfoListByIds(List<Long> ids) {
		DrmCourseExample dce = new DrmCourseExample();
		dce.createCriteria().andCourseIdIn(ids);
		List<DrmCourse> drs = drmCourseMapper.selectByExample(dce);
		return drs;

		// List<CourseInfoBean> result = new ArrayList<CourseInfoBean>();
		// if(drs!=null){
		// for(DrmCourse dc :drs){
		// CourseInfoBean cinf = new CourseInfoBean();
		// BeanUtils.copyProperties(dc, cinf);
		// result.add(cinf);
		// }
		// }
		// return result;
	}

	public List<DrmCourse> getCourseByCartIds(Long cartId[]) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("cartIds", cartId);
		return drmCourseMapper.getCourseByCartIds(param);
	}

	@Override
	public List<CourseRecommendVo> getRecommendPublicCourseInfoBeanList(
			Map<String, Object> mapInfo) {
		
		return courseInfoMapper.getRecommendPublicCourseInfoBeanList(mapInfo);
	}

	@Override
	public List<DrmCourse> getCourseInfoByType(
			Map<String, Object> searchMap) {
		return drmCourseMapper.getCourseByType(searchMap);
	}

}
